package ch.usi.inf.pf2.CSV;

import java.io.*;

import ch.usi.inf.pf2.spreadsheet.SpreadSheetCellArray;
import ch.usi.inf.pf2.spreadsheet.SpreadSheetCellArray;

public class FileWrite {
	
	public static int writeFile (SpreadSheetCellArray spread, String file) {
		try {
			BufferedWriter fw = new BufferedWriter(new FileWriter(new File (file)));
			for (int row = 0; row < spread.getRowCount(); row++) {
				String line = spread.getValue(row, 0);
				for (int column = 1; column < spread.getColumnCount(); column++) {
					line = line + "," + spread.getValue(row, column);
				}
				fw.write(line + "\n");
			}
			fw.close();
			return Message.SUCCESS;
		} catch (IOException ex) {
			return Message.ERROR_READING;
		}
	}
}